// pages/record/record.js
const app = getApp()
import util, { route } from '../../utils/util'
let that;
Page({
  data: {
    time: null,
    startTime: '2021-1-1',
    babaColor: [{ c_id: 0, text: '黑色' }, { c_id: 1, text: '咖啡色' }, { c_id: 2, text: '绿色' }, { c_id: 3, text: '黄色' }],
    babaHard: [{ h_id: 0, text: '水' }, { h_id: 1, text: '稀' }, { h_id: 2, text: '适中' }, { h_id: 3, text: '硬哦' }],
    babaShape: [{ s_id: 0, text: '一滩' }, { s_id: 1, text: '粥状' }, { s_id: 2, text: '条条' }, { s_id: 3, text: '大力金刚丸' }],
  },
  onLoad: function (options) {
    if (options.time != {}) {
      let time = JSON.parse(options.data)
      time.minutes = time.minutes > 10 ? time.minutes : '0' + time.minutes
      time.seconds = time.seconds > 10 ? time.seconds : '0' + time.seconds
      this.setData({ time })
    }
  },
  onShow: function () {
    this.getTime()
  },
  getTime() {
    that = this;
    let nowDate = new Date()
    let years = nowDate.getFullYear()
    let months = nowDate.getMonth() + 1
    let dates = nowDate.getDate()
    months = months < 10 ? '0' + months : months
    dates = dates < 10 ? '0' + dates : dates
    let endTime = years + '-' + months + '-' + dates
    that.setData({ endTime, sleDate: endTime })
  },
  bindDateChange: function (e) {
    this.setData({
      sleDate: e.detail.value
    })
  },
  // 颜色选择
  colorRadio(e) {
    let babaColor = this.data.babaColor
    let c_id = e.detail.value
    babaColor.forEach(item => {
      babaColor[item.c_id].checked = c_id == item.c_id ? true : false
    })
    this.setData({ babaColor })
  },
  // 硬度选择
  hardRadio(e) {
    let babaHard = this.data.babaHard
    let h_id = e.detail.value
    babaHard.forEach(item => {
      babaHard[item.h_id].checked = h_id == item.h_id ? true : false
    })
    this.setData({ babaHard })
  },
  // 形状选择
  shapeRadio(e) {
    let babaShape = this.data.babaShape
    let s_id = e.detail.value
    babaShape.forEach(item => {
      babaShape[item.s_id].checked = s_id == item.s_id ? true : false
    })
    this.setData({ babaShape })
  },
  // 提交
  formSubmit(e) {
    let ui = wx.getStorageSync('userInfo');
    let formdata = e.detail.value
    if (!ui.openid) {
      route('/pages/mine/mine', {}, 'switchtab')
    } else {
      wx.cloud.callFunction({
        name: 'addrecord',
        data: {
          colorRadio: formdata.colorRadio,
          hardRadio: formdata.hardRadio,
          minutes: formdata.minutes,
          money: formdata.money,
          seconds: formdata.seconds,
          shapeRadio: formdata.shapeRadio,
          sleDate: formdata.sleDate,
          openid: ui.openid,
          date: new Date()
        },
        success: res => {
          console.log('粑粑日记提交成功');
          util.route('/pages/detail/detail', {},'redto')
        }, fail: err => {
          console.log(err);
        }
      })
    }
  }
})